package cn.itcast.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();


		String username = request.getParameter("username");
		String password = request.getParameter("password");

		List<User> uses = Db.getAll();

		for (User user : uses) {
			if (username!=null && user.getUsername().equals(username)&& password!=null &&user.getPassword().equals(password)) {

				HttpSession session = request.getSession();
				session.setAttribute("user", user);

				response.sendRedirect("/day07/index.jsp");
				return ;

			}
		}


		out.print("用户名,密码 不正确");




	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
		response.setContentType("text/html;charset=UTF-8");

	}

}


class  Db{

	private   static List<User> list = new ArrayList<User>();


	static{

		list.add(new User("aaa","123"));
		list.add(new User("bbb","123"));
		list.add(new User("ccc","123"));


	}


	public static List  getAll(){
		return list;
	}

}

class User{

	private String username;
	private String password;



	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}



	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}










}

